﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.Base;
using LJX.SmartPark.Domain.IotInfo;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJX.SmartPark.Service.IotInfo.SmartLamp
{
    public class SmartLampService : ISmartLampService  
    {
        private readonly IRepository<Domain.IotInfo.SmartLamp.SmartLamp> _SmartLampsService;

        public SmartLampService(IRepository<Domain.IotInfo.SmartLamp.SmartLamp> SmartLampsService)
        {
            _SmartLampsService = SmartLampsService;
        }

        Domain.IotInfo.SmartLamp.SmartLamp SmartLampss = new Domain.IotInfo.SmartLamp.SmartLamp();
        //智能灯列表显示
        public async Task<Response<List<Domain.IotInfo.SmartLamp.SmartLamp>>> GetSmartLampsAsync()
        {
            var SmartLamps = await _SmartLampsService.GetAllListAsync();
            var result = SmartLamps.MapToList<Domain.IotInfo.SmartLamp.SmartLamp>();
            return Response<List<Domain.IotInfo.SmartLamp.SmartLamp>>.Ok(result);
        }
        //智能灯列表显示根据设备id查询
        public Response<Domain.IotInfo.SmartLamp.SmartLamp> GetSmartLampsData(Domain.IotInfo.SmartLamp.SmartLamp input)
        {
            var SmartLamps = _SmartLampsService.GetListQuery().FirstOrDefault(m => m.Id.ToString() == input.UnitType);
            var result = SmartLamps.MapTo<Domain.IotInfo.SmartLamp.SmartLamp>();
            return Response<Domain.IotInfo.SmartLamp.SmartLamp>.Ok(result);
        }
        //智能灯添加
        public async Task<Response<bool>> SmartLampsAdd(Domain.IotInfo.SmartLamp.SmartLamp input)
        {
            var result = _SmartLampsService.InsertAsyncById(input.MapTo<Domain.IotInfo.SmartLamp.SmartLamp>());

            return Response<bool>.Ok(result.Result > 0);
        }
        //智能灯删除
        public async Task<Response<bool>> SmartLampsDelete(Domain.IotInfo.SmartLamp.SmartLamp input)
        {
            var result = await _SmartLampsService.DeleteByIdAsync(input.MapTo<Domain.IotInfo.SmartLamp.SmartLamp>());
            return Response<bool>.Ok(result > 0);
        }

        //智能灯修改
        public async Task<Response<bool>> SmartLampsUpdate(Domain.IotInfo.SmartLamp.SmartLamp input)
        {
            var result = _SmartLampsService.UpdateAsync(input.MapTo<Domain.IotInfo.SmartLamp.SmartLamp>());

            return Response<bool>.Ok(result.Result > 0);
        }
        //智能灯批量删除
        public async Task<Response<bool>> SmartLampsDeleteDatas(string[] ids)
        {
            
            var dtos = _SmartLampsService.GetListQuery().Where(m => ids.Contains(m.Id.ToString()));
            var result = await _SmartLampsService.DeleteAsync(dtos.ToArray());
            return Response<bool>.Ok(result>0);
        }
        //总条数
        public int GetCount()
        {
            return _SmartLampsService.GetCount();
        }


        //显示分页查询
        public async Task<Response<List<SmartPark.Domain.IotInfo.SmartLamp.SmartLamp>>> GetSmartLampsPage(SelectLog select)
        {
            var datas = await _SmartLampsService.GetAllListAsync();
            var result = datas.MapToList<Domain.IotInfo.SmartLamp.SmartLamp>();
            //设备编号查询或设备型号查询
            if (!string.IsNullOrWhiteSpace(select.typeId))
            {
                    result = result.Where(x => x.TypeId.ToString() == select.typeId ).ToList();
            }
            if ( !string.IsNullOrWhiteSpace(select.UnitType))
            {
                result = result.Where(x => x.UnitType.Contains(select.UnitType)).ToList();
            }
            var totalCount = result.Count();

            result = result.Skip(((select.pageIndex - 1) * select.pageSize)).Take(select.pageSize).ToList();

            return Response<List<SmartPark.Domain.IotInfo.SmartLamp.SmartLamp>>.Ok(result, totalCount);
        }

        //页数条数model类
        public class SelectLog
        {
            public int pageIndex { get; set; }
            public int pageSize { get; set; }
            public string typeId { get; set; }
            public string UnitType { get; set; }
        }

    }
}
